package com.catazzz.springbootstudy.common.rabbit;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @作者: Tao
 * @日期: 2025/1/13
 * @时间: 14:49
 * @描述: 路由模式-Direct交换机-消费者<br><pre>
 */
@Slf4j
@Component
public class DirectConsumer {

    /**
     * receiveOne 方法用于接收消息<br><pre>
     *
     * 它绑定到一个名为 "direct" 的直接交换机，并监听 "liushuijing" 和 "shuijing" 路由键的消息
     * 当有匹配路由键的消息到达时，该方法会被调用，处理接收到的消息
     *
     * @param message 接收到的消息内容
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            key = {"liushuijing", "shuijing"},
            exchange = @Exchange(name = "direct", type = "direct")))
    public void receiveOne(String message) {
        log.info("DirectConsumer receiveOne message: {}", message);
    }

    /**
     * receiveTwo 方法也用于接收消息，与 receiveOne 方法并行处理<br><pre>
     * 它具有与 receiveOne 相同的绑定配置，用于负载均衡或消息分发策略
     * 当有匹配路由键的消息到达时，该方法会被调用，处理接收到的消息
     *
     * @param message 接收到的消息内容
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            key = {"liushuijing", "shuijing"},
            exchange = @Exchange(name = "direct", type = "direct")))
    public void receiveTwo(String message) {
        log.info("DirectConsumer receiveTwo message: {}", message);
    }
}
